define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hotel/index' + location.search,
                    add_url: 'hotel/add',
                    edit_url: 'hotel/edit',
                    del_url: 'hotel/del',
                    multi_url: 'hotel/multi',
                    import_url: 'hotel/import',
                    table: 'hotel',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'hotel_name', title: __('Hotel_name'), operate: 'LIKE'},
                        {field: 'hotel_contact', title: __('Hotel_contact'), operate: 'LIKE'},
                        {field: 'hotel_phone', title: __('Hotel_phone'), operate: 'LIKE'},
                        {field: 'hotel_license_image', title: __('Hotel_license_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'hotel_image', title: __('Hotel_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status_real', title: __('Status_real'), searchList: {"1":__('Status_real 1'),"2":__('Status_real 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('简介'),
                                    title: __('简介'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'hotel/content',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        return row.status === 1;
                                    }
                                },
                                {
                                    name: 'disable',
                                    text: __('禁用'),
                                    title: __('禁用'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    refresh: true,
                                    hidden: function (row, o) {
                                        if (row.status === 2) return true
                                    },
                                    confirm: '即将禁止该商家在平台筛选报名和参加工作，请确认是否禁用！',
                                    url: 'hotel/disable',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                                {
                                    name: 'enable',
                                    text: __('启用'),
                                    title: __('启用'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    refresh: true,
                                    hidden: function (row, o) {
                                        if (row.status === 1) return true
                                    },
                                    url: 'hotel/enable',
                                    success: function (data) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                },
                            ]
                        },
                    ]
                ],
            });

            // 批量启用
            $(".btn-normal").on("click", function () {
                let data = table.bootstrapTable('getSelections');
                let ids = data.map(x => {
                    return x.id
                }).join(',')
                $.post({
                    url: 'hotel/batch?act=1&ids=' + ids,
                    success: function (res) {
                        if (res.code === 0) {
                            Toastr.error(res.msg);
                        } else {
                            Toastr.success(res.msg);
                        }
                        $(".btn-refresh").trigger("click");
                    }
                })
            })

            // 批量禁用
            $(".btn-hidden").on("click", function () {
                let data = table.bootstrapTable('getSelections');
                let ids = data.map(x => {
                    return x.id
                }).join(',')
                $.post({
                    url: 'hotel/batch?act=2&ids=' + ids,
                    success: function (res) {
                        if (res.code === 0) {
                            Toastr.error(res.msg);
                        } else {
                            Toastr.success(res.msg);
                        }
                        $(".btn-refresh").trigger("click");
                    }
                })
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        content:function (){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});